<?php

	$action=trim($_POST['action']);	
	if($action=="buy")
	{
		$out='请求失败...';
		
		$userID=GetLoginUserID();
		if($userID===false)
		{
			$out='用户未登陆...';
			echo $out;
			return;
		}
		$giftId=intval($_POST['couponID']);
		if($giftId<=0)
		{
			$out='不合法的优惠券...';
			echo $out;
			return;
		}
		
		$giftDAO=GetGiftCouponDAO();
		$coupon=null;
		$result=$giftDAO->BuyCoupon($userID, $giftId, $coupon);
		
		switch($result)
		{
			case 1:
			{
				//获取coupon成功
				/*
				$out="<b>恭喜你，兑换成功！</b><br/>以下是您购买 的优惠券信息，请在有效期内尽快使用，请勿将密码泄露给他人。<br/>".
				$coupon['coupon_desc']."<br/>有效期：".EdpSubStr($coupon['coupon_start_date'],0,10)."至".EdpSubStr($coupon['coupon_end_date'],0,10).
				"<br/>密码：<input id=\"couponKeyResult\" type=\"text\" value=\"".$coupon['coupon_key']."\"/ disabled = \"disabled\">
				<input type=\"button\" value=\"复制\" onclick=\"CopyToClipboard($('#couponKeyResult').val())\">
				<br/>消费地址：<input id=\"couponUrlResult\" type=\"text\" value=\"".$coupon['coupon_url']."\"/ disabled = \"disabled\">
				<input type=\"button\" value=\"复制\" onclick=\"CopyToClipboard($('#couponUrlResult').val())\">";
				*/
				
				$out="<b>恭喜你，兑换成功！</b><p>有效期：".EdpSubStr($coupon['coupon_start_date'],0,10)." 至 ".EdpSubStr($coupon['coupon_end_date'],0,10).
				"</p><p>密码：<input id=\"couponKeyResult\" type=\"text\" value=\"".$coupon['coupon_key']."\"/ disabled = \"disabled\" style=\"width:200px\">
				<input type=\"button\" value=\"复制\" onclick=\"CopyToClipboard($('#couponKeyResult').val())\" style=\"height:24px\">
				<p/>";
								
				break;
			}
			case 2:
			{
				$out="优惠券已过期或已售完...";
				break;
			}
			case 3:
			{
				$out="对不起，您兑换的数目超过的库存上限...";
				break;
			}
			case 4:
			{
				$out="对不起,您现有的E币数目不够进行兑换...";
				break;
			}
			case 5:
			{
				$out="内部错误...";
				break;
			}
			default:
				break;
		}
		
		echo $out;
		return;
	}
?>